#ifndef TREEITEM_H
#define TREEITEM_H

#include <QList>
#include <QMap>
#include <QVariant>
#include <QVector>
#include <QFile>
#include <QImage>

#include "Deck.h"
#include "Card.h"
#include "CardInfo.h"


class KnowledgeBase;
class ITreeItemFactory;

class TreeItem
{
	public:

		enum DataColumn
		{
			QuestionColumn,
			AnswerColumn,
			ColumnsCount
		};

		typedef QMap<int, QImage> ImageMap;

	public:

		//TreeItem(const QVector<QVariant> &data, TreeItem *parent);
		explicit				TreeItem(TreeItem * parent);
		~TreeItem();

		friend QDataStream		&operator<<( QDataStream &stream, const TreeItem &data );
		//friend QDataStream		&operator>>( QDataStream &stream, TreeItem &data );

		void						readVer0(QDataStream &stream);
		void						readFrom(QDataStream &stream); //currentVersion 1

		TreeItem					*child(int number);
		int							childCount() const;
		unsigned int				getNumCards() const;

		bool						hasChildren() const;
		int							columnCount() const;
		virtual QVariant			data(int column) const;
		bool						insertChildren(int position, int count, ITreeItemFactory* factory =0);
		TreeItem					*parent();
		const TreeItem				*parent() const;
		bool						removeChildren(int position, int count);
		bool						cutChildren(int position, int count);
		bool						paste(int position, int count);

		int							childNumber() const;
		virtual bool				setData(int column, const QVariant &value);

		Card	*					getCard() {
			return card;
		}

		bool						isInDeck();

		bool						isCard() const;
		void						setIsCard(bool);

		bool						isDue() const;
		int							getDueDate() const;

		void						getDueCardsDepthFirst(TreeItem*&, CardInfo&, QList<QString> lineage) const;

		void						invert();

		bool						hasCards() const;

		void						setIsPaused(bool);
		bool						isPaused() const;

		void						getLineage(QList<QString>& lineage) const;

		bool						hasImage(DataColumn col) const;
		QImage						image(DataColumn col) const;
		void						setImage(DataColumn col, QImage value);
		void						resetImage(DataColumn col);

		QString						questionText() const;
		void						setQuestionText(const QString& value);

		QString						answerText() const;
		void						setAnswerText(const QString& value);

		bool						setText(DataColumn col, const QString& value);
		QString						getText(DataColumn col) const;

		Deck*						getDeck() const;

		KnowledgeBase*				knowledgeBase() const;
		void						setKnowledgeBase(KnowledgeBase* value);

		int							indexOfChild(TreeItem* childItem) const;

		QUuid						getUuid() const {
			return uuid;
		}

	private:

		QString					questionField() const;
		QString					answerField() const;
		void					setQuestionField(const QString& value);
		void					setAnswerField(const QString& value);

		void					readItemData(QDataStream& stream);

	private:

		QUuid					uuid;
		QList<TreeItem*>			childItems;
		//QVector<QString>		itemData;
		QString					_question;
		QString					_answer;
		TreeItem*				parentItem;
		Card*					card;
		bool						iscard;
		bool						ispaused;

		ImageMap				imgMap;

		KnowledgeBase*			m_pKnowledgeBase;
};

#endif
